/**
 * 
 */
package com.yidu.view;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.bean.TbMember;
import com.yidu.bean.TbSite;
import com.yidu.commom.Tool;
import com.yidu.message.Message;
import com.yidu.service.TbSiteService;

/**
 * 类的描述 -- 类
 * @author LiuZongNan
 * 2018年7月18日
 */
@Controller
@RequestMapping("/site")
public class TbSiteAction {
	//控制台打印
	Logger logger = Logger.getLogger(TbSiteAction.class);
	
	//消息信息类
	Message msg = new Message();

	//json
	ObjectMapper mapper = new ObjectMapper();
	
	@Resource
	private TbSiteService siteService; 
	
	@RequestMapping("/skip")
	public String skip(){
		return "background/center/site";
	}
	/**
	 * 根据用户ID查询该用户的收货地址
	 * @param memId 用户ID
	 * @return
	 */
	@RequestMapping("/findByMenId")
	@ResponseBody
	public List<TbSite> findByMenId(HttpSession session,String memId){
		TbMember member = (TbMember) session.getAttribute("user");
		memId = member.getMemId();
		logger.info("findById:根据用户id查询配送地址的方法");
		List<TbSite> site = siteService.findByMenId(memId);
		return site;
	}
	/**
	 * 增加或者修改配送地址的方法
	 * @param request
	 * @param site
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Message add(HttpSession session,TbSite tbSite){
		TbSite site = new TbSite();
		TbMember member = (TbMember) session.getAttribute("user");
		site.setMemId(member.getMemId());
		site.setSiteId(UUID.randomUUID().toString());
		site.setSiteName(tbSite.getSiteName());
		site.setSiteSex(tbSite.getSiteSex());
		site.setSite(tbSite.getSite());
		site.setPhone(tbSite.getPhone());
		int add = siteService.save(site);
		if(add>0){
			msg.setMsg("增加成功");
			msg.setNum(1);
			return msg;
		}else{
			msg.setMsg("增加失败");
			msg.setNum(0);
			return msg;
		}
		
	}
	/*@RequestMapping(value="/saveOrUpdate",method=RequestMethod.POST)
	@ResponseBody
	public Object saveOrUpdate(HttpServletRequest request,TbSite site) {
		logger.info("saveOrUpdate:增加或者修改配送地址的方法");
		//判断是否存在配送地址ID
		int num = 0;
		if(!Tool.isEmpty(site.getMemId())){
			//存在配送地址ID，执行修改配送地址
			logger.info("存在配送地址ID，执行修改配送地址方法。。。。。。");
			num = siteService.update(site);
		}else{
			//不存在配送地址ID，执行增加配送地址
			logger.info("不存在配送地址ID，执行增加配送地址方法。。。。。。");
			//自行增加配送地址ID（主键）
			site.setSiteId(Tool.getRandomString(32));
			num = siteService.save(site);
		}
		//根据 操作返回
		msg.setNum(num);
		if(num>0){
			msg.setMsg("操作成功!");
		}else{
			msg.setMsg("操作失败!");
		}
		return msg;
	}*/
	@RequestMapping("/delete")
	@ResponseBody
	public Object delect(String siteId){
		logger.info("delect:删除配送地址方法....");
		TbSite site = new TbSite();
		siteId = site.getSiteId();
		int row = siteService.delete(siteId);
		if(row>0){
			msg.setMsg("删除成功!");
		}else{ 
			msg.setMsg("删除失败!");
		} 
		return msg;
	}
}
